/*
 * @Author: Marte
 * @Date:   2016-10-24 16:55:45
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-10 15:51:48
 */

'use strict';

var currApp = angular.module('index.app', ['qf.common']);

currApp.controller('pagectrl', ['$scope', '$http', '$window', 'dialogService',

    function($scope, $http, $window, dialogService) {
        $scope.globalParam = globalParam;

        var vm = this;
        vm.typeRemark = {
            remark: {},
            type: {}
        };

        /*获取证件类型*/
        $http.get('api/types')
            .success(function(data, status, headers, config) {
                var types = data;
                vm.types = types.error ? null : types;
            })
            .error(function(data, status, headers, config) {
                vm.types = null;
            });

        /*获取评语*/
        $http.get('api/remarks')
            .success(function(data, status, headers, config) {
                var remarks = data;
                vm.remarks = remarks.error ? null : remarks;
            })
            .error(function(data, status, headers, config) {
                vm.remarks = null;
            });

        vm.isCurrType = function(typeId) {
            return typeId && vm.currTypeId == typeId;
        };

        vm.isCurrRemark = function(remarkId) {
            return remarkId && vm.currRemarkId == remarkId;
        };

        /*检查类型是否可用*/
        vm.checkType = function(checkTypeId) {
            if (!vm.currRemarkId) {
                return true;
            } else {
                return vm.typeRemark.remark[vm.currRemarkId] && vm.typeRemark.remark[vm.currRemarkId].indexOf(checkTypeId) > -1;
            }
        };
        /*检查评语是否可用*/
        vm.checkRemark = function(checkRemarkId) {
            if (!vm.currTypeId) {
                return true;
            } else {
                return vm.typeRemark.type[vm.currTypeId] && vm.typeRemark.type[vm.currTypeId].indexOf(checkRemarkId) > -1;
            }
        };

        /*重新选择，清除选中*/
        $scope.reset = function() {
            vm.currTypeId = null;
            vm.currRemarkId = null;
        };

        /*选择类型*/
        $scope.chooseType = function(event) {
            var chooseTypeId = parseInt(event.target.getAttribute('data-id'));
            //是可用类型才往下执行
            if (vm.checkType(chooseTypeId)) {
                vm.currTypeId = chooseTypeId;
                vm.currType = event.target.getAttribute('data-name');
                /*选择类型后获取评语*/
                //如果已经有获取过则不再获取
                if (!vm.typeRemark.type[vm.currTypeId]) {
                    $http.get('api/remarksbytype/' + vm.currTypeId)
                        .success(function(data, status, headers, config) {
                            var remarks = data;
                            if (!remarks.error) {
                                var array = [];
                                for (var i = 0; i < remarks.length; i++) {
                                    array.push(remarks[i].REMARKID);
                                }
                                vm.typeRemark.type[vm.currTypeId] = array;
                            }
                        })
                        .error(function(data, status, headers, config) {
                            vm.typeRemark.type[vm.currTypeId] = null;
                        });
                }
            }
        };

        /*选择评语*/
        $scope.chooseRemark = function(event) {
            var chooseRemarkId = parseInt(event.target.getAttribute('data-id'));
            if (vm.checkRemark(chooseRemarkId)) {
                vm.currRemarkId = event.target.getAttribute('data-id');
                vm.currRemark = event.target.getAttribute('data-name');
                /*选择评语后获取类型*/
                //如果已经有获取过则不再获取
                if (!vm.typeRemark.remark[vm.currRemarkId]) {
                    $http.get('api/typesbyremark/' + vm.currRemarkId)
                        .success(function(data, status, headers, config) {
                            var types = data;
                            if (!types.error) {
                                var array = [];
                                for (var i = 0; i < types.length; i++) {
                                    array.push(types[i].TYPEID);
                                }
                                vm.typeRemark.remark[vm.currRemarkId] = array;
                            }
                        })
                        .error(function(data, status, headers, config) {
                            vm.typeRemark.remark[vm.currRemarkId] = null;
                        });

                }
            }
        };

        //进入图片列表
        $scope.gotoList = function() {
            //必须同时选择了证件类型和评语才允许进入图片列表页
            if (vm.currTypeId && vm.currRemarkId) {
                $window.location.href = 'picturelist.html?' + globalParam.paramString() + '&typeid=' + vm.currTypeId + '&remarkid=' + vm.currRemarkId + '&type=' + vm.currType + '&remark=' + vm.currRemark;
            }
            if (!vm.currTypeId) {
                dialogService.showmessage({ msg: '请选择证件类型！' });
            } else if (!vm.currRemarkId) {
                dialogService.showmessage({ msg: '请选择评语！' });
            }
        };

        //console.log('启动成功');

    }
]);
